﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Batman.Cms.Domains;
using Batman.Cms.Services.Applications.Filters;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;

namespace Batman.Cms.Services.Applications
{
    public class ApplicationDataItemReader : ApplicationService
    {
        public IRepository<ApplicationDataItem> repository { get; set; }

        public PagedResultDto<ApplicationDataItem> Get(PagedResultRequestDto page, ApplicationDataItemFilter filter)
        {
            var query = repository.GetAll().AsNoTracking();
            if (filter.ApplicationDataId.HasValue)
            {
                query = query.Where(t => t.ApplicationDataId == filter.ApplicationDataId);
            }

            var items = query.OrderByDescending(t => t.Id).Skip(page.SkipCount).Take(page.MaxResultCount).ToList();
            var total = query.Count();

            return new PagedResultDto<ApplicationDataItem>(total, items);
        }

        public List<ApplicationDataItem> Get(ApplicationDataItemFilter filter)
        {
            var query = repository.GetAll().AsNoTracking();
            if (filter.ApplicationDataId.HasValue)
            {
                query = query.Where(t => t.ApplicationDataId == filter.ApplicationDataId);
            }

            var items = query.OrderByDescending(t => t.Id).ToList();
            return items;
        }
    }
}
